package com.mmy.controller.restaurant;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.domain.restaurant.TRestaurantOrder;
import com.mmy.domain.restaurant.TRestaurantPlan;
import com.mmy.domain.restaurant.VRestaurantBase;
import com.mmy.domain.restaurant.VRestaurantPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.restaurant.TRestaurantBaseServiceImpl;
import com.mmy.service.impl.restaurant.TRestaurantOrderServiceImpl;
import com.mmy.service.impl.restaurant.TRestaurantPlanServiceImpl;
import com.mmy.service.impl.restaurant.VRestaurantBaseServiceImpl;
import com.mmy.service.impl.restaurant.VRestaurantPlanServiceImpl;

/**
 * 类的介绍：餐馆订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class TRestaurantOrderController {

	@Autowired
	TRestaurantOrderServiceImpl tRestaurantOrderServiceImpl;
	@Autowired
	TRestaurantBaseServiceImpl tRestaurantBaseServiceImpl;
	@Autowired
	TRestaurantPlanServiceImpl tRestaurantPlanServiceImpl;
	@Autowired
	VRestaurantPlanServiceImpl vRestaurantPlanServiceImpl;
	@Autowired
	VRestaurantBaseServiceImpl vRestaurantBaseServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 新建餐馆预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterreserverestaurant")
	public String EnterReserveRestaurant(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));

		List<VRestaurantPlan> lstRestaurantPlan = vRestaurantPlanServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("restaurantplanlist", lstRestaurantPlan);

		model.addAttribute("iRestaurantID", iRestaurantID);

		return "webbackground/restaurantmanagement/reserverestaurant";

	}

	@RequestMapping(value = "/subreserverestaurant")
	public String ReserveRestaurant(TRestaurantOrder tRestaurantOrder, HttpSession session, String PlanID,
			String RestaurantID) throws Exception {
		Object oID = tRestaurantOrderServiceImpl.getMaxID();
		if (oID == null) {
			oID = 1;
			tRestaurantOrder.setID(1);

		} else {
			oID = (int) oID + 1;
			tRestaurantOrder.setID((int) oID);
		}
		/* 获得订单编号 */
		// 格式化日期为"yymmdd"
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		StringBuffer orderIDBuffer = new StringBuffer();
		orderIDBuffer.append(RestaurantID);
		orderIDBuffer.append(PlanID);
		orderIDBuffer.append(format.format(date));
		if ((int) oID == 1) {
			orderIDBuffer.append(1);
		} else {
			orderIDBuffer.append((int) oID);
		}
		String cOrderID = orderIDBuffer.toString();
		int iPlanID = Integer.parseInt(PlanID);
		TRestaurantPlan tRestaurantPlan = tRestaurantPlanServiceImpl.findByPlanID(iPlanID);
		tRestaurantOrder.setCreateDate(new Date());
		tRestaurantOrder.setReserveDate(new Date());
		tRestaurantOrder.setFloor(tRestaurantPlan.getFloor());
		tRestaurantOrder.setPeer(tRestaurantPlan.getPeer());
		tRestaurantOrder.setPrice(tRestaurantPlan.getPrice());
		BigDecimal bPrice = tRestaurantPlan.getPrice();
		tRestaurantOrder.setOrderAmount(bPrice);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		tRestaurantOrder.setCreator(tSysUser.getUserName());
		tRestaurantOrder.setReserveUserID(tSysUser.getUserID());
		tRestaurantOrder.setOrderID(cOrderID);
		tRestaurantOrderServiceImpl.save(tRestaurantOrder.getID());
		tRestaurantOrder.setOrderState((byte) 0);
		tRestaurantOrder.setCashierState((byte) 0);
		tRestaurantOrder.setAccountState((byte) 0);
		tRestaurantOrder.setInvoiceState((byte) 0);
		tRestaurantOrder.setRefundState((byte) 0);
		tRestaurantOrder.setRemittanceState((byte) 0);
		tRestaurantOrderServiceImpl.save(tRestaurantOrder);
		return "rest/RestIndex";
	}

	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayorderrestaurant")
	public String EnterPayOrderRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		return "webbackground/restaurantmanagement/payorderrestaurant";

	}

	@RequestMapping(value = "/subpayorderrestaurant")
	public String PayOrderRestaurant(TRestaurantOrder tRestaurantOrder, HttpSession session, String RestaurantID)
			throws Exception {

		try {
//			 定义一个新的实体类对象 
			TRestaurantOrder dtRestaurantOrder = tRestaurantOrderServiceImpl.findByID(tRestaurantOrder.getID());

			dtRestaurantOrder.setPayType(tRestaurantOrder.getPayType());
			dtRestaurantOrder.setOrderState((byte) 2);
			dtRestaurantOrder.setOrderDate(new Date());
			dtRestaurantOrder.setOrderName(tRestaurantOrder.getOrderName());
			dtRestaurantOrder.setOrderTel(tRestaurantOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtRestaurantOrder.setOrderUserID(tSysUser.getUserID());
			dtRestaurantOrder.setReceivable(tRestaurantOrder.getReceivable());
			dtRestaurantOrder.setVoucher(tRestaurantOrder.getVoucher());
			dtRestaurantOrder.setReceipt(tRestaurantOrder.getReceipt());
			tRestaurantOrderServiceImpl.save(dtRestaurantOrder);

			return "redirect:printrestaurant?iID=" + tRestaurantOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelorderrestaurant")
	public String EnterCancelOrderRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		return "webbackground/restaurantmanagement/cancelorderrestaurant";

	}

	@RequestMapping(value = "/subcancelorderrestaurant")
	public String CancelOrderRestaurant(TRestaurantOrder tRestaurantOrder, HttpSession session, String RestaurantID)
			throws Exception {

		try {
//			 定义一个新的实体类对象 
			TRestaurantOrder dtRestaurantOrder = tRestaurantOrderServiceImpl.findByID(tRestaurantOrder.getID());

			dtRestaurantOrder.setOrderState((byte) 4);
			dtRestaurantOrder.setRefund(tRestaurantOrder.getRefund());
			tRestaurantOrderServiceImpl.save(dtRestaurantOrder);

			return "redirect:printcancelrestaurant?iID=" + tRestaurantOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 修改餐馆预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereserverestaurant")
	public String EnterUpdateReserveScenic(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);
		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		return "webbackground/restaurantmanagement/updatereserverestaurant";

	}

	@RequestMapping(value = "/updatereserverestaurant")
	public String UpdateReserveRestaurant(TRestaurantOrder tRestaurantOrder, HttpSession session, String RestaurantID)
			throws Exception {
		try {
//			 定义一个新的实体类对象 
			TRestaurantOrder dtRestaurantOrder = tRestaurantOrderServiceImpl.findByID(tRestaurantOrder.getID());
			dtRestaurantOrder.setReserveName(tRestaurantOrder.getReserveName());
			dtRestaurantOrder.setReserveTel(tRestaurantOrder.getReserveTel());
			dtRestaurantOrder.setMemo(tRestaurantOrder.getMemo());
			dtRestaurantOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtRestaurantOrder.getPrice();
			dtRestaurantOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtRestaurantOrder.setReviser(tSysUser.getUserName());
			tRestaurantOrderServiceImpl.save(dtRestaurantOrder);
			return "redirect:restaurantbaseinfo?iRestaurantID=" + Integer.parseInt(RestaurantID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreserverestaurant")
	public String CancelReserveRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);

		tRestaurantOrder.setOrderState((byte) 3);

		tRestaurantOrderServiceImpl.save(tRestaurantOrder);

		model.addAttribute("tRestaurantOrder", tRestaurantOrder);
		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		VRestaurantBase vRestaurantBase = vRestaurantBaseServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("vRestaurantBase", vRestaurantBase);

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		return "webbackground/restaurantmanagement/restaurantinfo";

	}

	/**
	 * 函数功能说明： 修改餐馆订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateorderrestaurant")
	public String EnterUpdateOrderRestaurant(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TRestaurantOrder tRestaurantOrder = tRestaurantOrderServiceImpl.findByID(iID);
		model.addAttribute("tRestaurantOrder", tRestaurantOrder);
		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		return "webbackground/restaurantmanagement/updateorderrestaurant";

	}

	@RequestMapping(value = "/updateorderrestaurant")
	public String UpdateOrderRestaurant(TRestaurantOrder tRestaurantOrder, HttpSession session, String RestaurantID)
			throws Exception {
		try {
//			 定义一个新的实体类对象 
			TRestaurantOrder dtRestaurantOrder = tRestaurantOrderServiceImpl.findByID(tRestaurantOrder.getID());
			dtRestaurantOrder.setOrderName(tRestaurantOrder.getOrderName());
			dtRestaurantOrder.setOrderTel(tRestaurantOrder.getOrderTel());
			dtRestaurantOrder.setMemo(tRestaurantOrder.getMemo());
			dtRestaurantOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtRestaurantOrder.getPrice();

			dtRestaurantOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtRestaurantOrder.setReviser(tSysUser.getUserName());
			tRestaurantOrderServiceImpl.save(dtRestaurantOrder);
			return "redirect:restaurantbaseinfo?iRestaurantID=" + Integer.parseInt(RestaurantID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreserverestaurant")
	public String DelReserveRestaurant(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tRestaurantOrderServiceImpl.delRestaurantOrder(lstID);

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);
		VRestaurantBase vRestaurantBase = vRestaurantBaseServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("vRestaurantBase", vRestaurantBase);

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		return "webbackground/restaurantmanagement/restaurantinfo";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delorderrestaurant")
	public String DelOrderRestaurant(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tRestaurantOrderServiceImpl.delRestaurantOrder(lstID);

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestaurantID"));
		model.addAttribute("iRestaurantID", iRestaurantID);

		VRestaurantBase vRestaurantBase = vRestaurantBaseServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("vRestaurantBase", vRestaurantBase);

		List<TRestaurantOrder> lstRestaurantOrder = tRestaurantOrderServiceImpl.findByRestaurantID(iRestaurantID);
		model.addAttribute("orderlist", lstRestaurantOrder);

		return "webbackground/restaurantmanagement/restaurantinfo";
	}

}
